﻿#ifndef QEXCEL_H
#define QEXCEL_H

#include <QString>
#include <QVariant>
#include <QColor>
//--------------wdg-------------------
//对齐方式
#define ALIGNMENT_CENTER   (-4108)
#define ALIGNMENT_LEFT     (-4131)
#define ALIGNMENT_RIGHT    (-4152)
#define ALIGNMENT_DEFAULT   1
//线的样式：0- no line; 1-solid; 2-big dot;3-small dot;4-dash dot; 5-dash dot dot;
#define LINE_STYLE_NO_LINE    0
#define LINE_STYLE_SOLID      1
#define LINE_STYLE_BIG_DOT    2
#define LINE_STYLE_SMALL_DOT  3
#define LINE_STYLE_DASH_DOT   4
#define LINE_STYLE_DASH_DOTDOT 5
//线的颜色 1-black;2-white;3-red;4-green;5-blue; 6-yellow; 7-pink;8-dark blue;
#define IDX_COLOR_BLACK        1
#define IDX_COLOR_WHITE        2
#define IDX_COLOR_RED          3
#define IDX_COLOR_GREEN        4
#define IDX_COLOR_BLUE         5
#define IDX_COLOR_YELLOW       6
#define IDX_COLOR_PINK         7


#define MASK_BIT_FONT_NAME           (unsigned char)0X01
#define MASK_BIT_FONT_SIZE           (unsigned char)0X02
#define MASK_BIT_FONT_COLOR          (unsigned char)0X04
#define MASK_BIT_FONT_BOLD           (unsigned char)0X08
#define MASK_BIT_FONT_ITALIC         (unsigned char)0X10
#define MASK_BIT_FONT_UNDERLINE      (unsigned char)0X20
#define MASK_BIT_FONT_SHADOW         (unsigned char)0X40
#define MASK_BIT_FONT_STRIKETHROUGH  (unsigned char)0X80
typedef struct tagRangeBorder
{
    unsigned char ucStyle; //线的样式：0- no line; 1-solid; 2-big dot;3-small dot;4-dash dot; 5-dash dot dot;
    unsigned char ucWidth;
    unsigned char ucColorIndex;//1-black;2-white;3-red;4-green;5-blue; 6-yellow; 7-pink;8-dark blue;
}T_RangeBorder;
typedef struct tagAlignment
{
    int H; /* ALIGNMENT_CENTER */
    int V;
}T_Alignment;
typedef struct tagCellFont
{
    unsigned char ucMask;
    QString strFontName;
    unsigned char ucFontSize;
    QColor dwFontColor;
    bool bBold;
    bool bItalic;
    bool bUnderline;
    bool bShadow;
    bool bStrikeThrough;
}T_RangeFont;
//--------------wdg end---------------
class QAxObject;

class QExcel : public QObject
{
public:
    QExcel(QString xlsFilePath, QObject *parent = 0);
    ~QExcel();
    QExcel(QObject *parent);
public:
    QAxObject * getWorkBooks();
    QAxObject * getWorkBook();
    QAxObject * getWorkSheets();
    QAxObject * getWorkSheet();

public:
    /**************************************************************************/
    /* 工作表                                                                 */
    /**************************************************************************/
    void selectSheet(const QString& sheetName);
    //sheetIndex 起始于 1
    void selectSheet(int sheetIndex);
    void deleteSheet(const QString& sheetName);
    void deleteSheet(int sheetIndex);
    void insertSheet(QString sheetName);
    int getSheetsCount();
    //在 selectSheet() 之后才可调用
    QString getSheetName();
    QString getSheetName(int sheetIndex);

    /**************************************************************************/
    /* 单元格                                                                 */
    /**************************************************************************/
    void setCellString(int row, int column, const QString& value);
    //cell 例如 "A7"
    void setCellString(const QString& cell, const QString& value);
    //range 例如 "A5:C7"
    void mergeCells(const QString& range);
    void mergeCells(int topLeftRow, int topLeftColumn, int bottomRightRow, int bottomRightColumn);
    QVariant getCellValue(int row, int column) const;
    void clearCell(int row, int column);
    void clearCell(const QString& cell);

    /**************************************************************************/
    /* 布局格式                                                               */
    /**************************************************************************/
    void getUsedRange(int *topLeftRow, int *topLeftColumn, int *bottomRightRow, int *bottomRightColumn);
    void setColumnWidth(int column, int width);
    void setColumnWidth(QString column, int width);
    void setRowHeight(int row, int height);
    void setCellTextCenter(int row, int column);
    void setCellTextCenter(const QString& cell);
    void setCellTextWrap(int row, int column, bool isWrap);
    void setCellTextWrap(const QString& cell, bool isWrap);
    void setAutoFitRow(int row);
    void setAutoFitColumn(int column);
    void mergeSerialSameCellsInAColumn(int column, int topRow);
    int getUsedRowsCount();
    void setCellFontBold(int row, int column, bool isBold);
    void setCellFontBold(const QString& cell, bool isBold);
    void setCellFontSize(int row, int column, int size);
    void setCellFontSize(const QString& cell, int size);

    /**************************************************************************/
    /* 文件                                                                   */
    /**************************************************************************/
    void save();
    void close();
    //===================added by wdg========================
    QAxObject * getRange(const QString &strCell1, const QString &strCell2 = "" );
    QAxObject * getRange(int minRow, int minCol, int maxRow=0, int maxCol=0);
    void setRangeFont(int minRow, int minCol, int maxRow, int maxCol, T_RangeFont * pFont);
    void setRangeWrap(int minRow, int minCol, int maxRow, int maxCol, bool bWrap = true);
    QString getCellText(int iRow, int iCol) const;
    void setRangeAlignment(int minRow, int minCol, int maxRow, int maxCol, T_Alignment * pAlignment);
    void setRangeBkColor(int minRow, int minCol, int maxRow, int maxCol, const QColor &colorRef);
    void setRangeBorder(int minRow, int minCol, int maxRow, int maxCol, T_RangeBorder * pRangeBorder =NULL );
    void drawGrid(int iminRow, int iminCol, int imaxRow, int imaxCol, T_RangeBorder * pRangeBorder = NULL );

    void showExcelApp(bool b);

    void setRangeFormat(int minRow, int minCol, int maxRow, int maxCol, const QString strFmt);
    //===============added end==============================
private:
    QAxObject * m_pExcel;
    QAxObject * m_pWorkBooks;
    QAxObject * m_pWorkBook;
    QAxObject * m_pSheets;
    QAxObject * m_pSheet;
    
    QString ColToColStr(int iCol);
    QString RowColToCellStr(int iRow, int iCol);
};

#endif
